package o;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Build.VERSION;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.VisibleForTesting;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.TreeMap;

@RequiresApi(19)
public class un implements um {
    private static final Config[] ˊ = new Config[]{Config.RGB_565};
    private static final Config[] ˋ;
    private static final Config[] ˎ = ˋ;
    private static final Config[] ˏ = new Config[]{Config.ALPHA_8};
    private static final Config[] ॱ = new Config[]{Config.ARGB_4444};
    private final Map<Config, NavigableMap<Integer, Integer>> ʻ = new HashMap();
    private final a ʼ = new a();
    private final ui<b, Bitmap> ᐝ = new ui();

    static /* synthetic */ class AnonymousClass3 {
        static final /* synthetic */ int[] ˊ = new int[Config.values().length];

        static {
            try {
                ˊ[Config.ARGB_8888.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                ˊ[Config.RGB_565.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                ˊ[Config.ARGB_4444.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                ˊ[Config.ALPHA_8.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
        }
    }

    @VisibleForTesting
    static class a extends tz<b> {
        protected /* synthetic */ ul ˏ() {
            return ˊ();
        }

        a() {
        }

        public b ˊ(int i, Config config) {
            b bVar = (b) ˋ();
            bVar.ˋ(i, config);
            return bVar;
        }

        protected b ˊ() {
            return new b(this);
        }
    }

    @VisibleForTesting
    static final class b implements ul {
        private Config ˊ;
        int ˋ;
        private final a ॱ;

        public b(a aVar) {
            this.ॱ = aVar;
        }

        public void ˋ(int i, Config config) {
            this.ˋ = i;
            this.ˊ = config;
        }

        public void ˎ() {
            this.ॱ.ॱ(this);
        }

        public String toString() {
            return un.ॱ(this.ˋ, this.ˊ);
        }

        public boolean equals(Object obj) {
            if (!(obj instanceof b)) {
                return false;
            }
            b bVar = (b) obj;
            if (this.ˋ == bVar.ˋ && aau.ॱ(this.ˊ, bVar.ˊ)) {
                return true;
            }
            return false;
        }

        public int hashCode() {
            return (this.ˊ != null ? this.ˊ.hashCode() : 0) + (this.ˋ * 31);
        }
    }

    static {
        Config[] configArr = new Config[]{Config.ARGB_8888, null};
        if (VERSION.SDK_INT >= 26) {
            configArr = (Config[]) Arrays.copyOf(configArr, configArr.length + 1);
            configArr[configArr.length - 1] = Config.RGBA_F16;
        }
        ˋ = configArr;
    }

    public void ˏ(Bitmap bitmap) {
        b ˊ = this.ʼ.ˊ(aau.ˏ(bitmap), bitmap.getConfig());
        this.ᐝ.ˋ(ˊ, bitmap);
        NavigableMap ˋ = ˋ(bitmap.getConfig());
        Integer num = (Integer) ˋ.get(Integer.valueOf(ˊ.ˋ));
        ˋ.put(Integer.valueOf(ˊ.ˋ), Integer.valueOf(num == null ? 1 : num.intValue() + 1));
    }

    @Nullable
    public Bitmap ॱ(int i, int i2, Config config) {
        ul ˎ = ˎ(aau.ॱ(i, i2, config), config);
        Bitmap bitmap = (Bitmap) this.ᐝ.ˋ(ˎ);
        if (bitmap != null) {
            ˎ(Integer.valueOf(ˎ.ˋ), bitmap);
            bitmap.reconfigure(i, i2, config);
        }
        return bitmap;
    }

    private b ˎ(int i, Config config) {
        ul ˊ = this.ʼ.ˊ(i, config);
        Config[] ˏ = ˏ(config);
        int length = ˏ.length;
        int i2 = 0;
        while (i2 < length) {
            Config config2 = ˏ[i2];
            Integer num = (Integer) ˋ(config2).ceilingKey(Integer.valueOf(i));
            if (num == null || num.intValue() > i * 8) {
                i2++;
            } else {
                if (num.intValue() != i || (config2 != null ? !config2.equals(config) : config != null)) {
                    this.ʼ.ॱ(ˊ);
                    return this.ʼ.ˊ(num.intValue(), config2);
                }
                return ˊ;
            }
        }
        return ˊ;
    }

    @Nullable
    public Bitmap ˊ() {
        Bitmap bitmap = (Bitmap) this.ᐝ.ˋ();
        if (bitmap != null) {
            ˎ(Integer.valueOf(aau.ˏ(bitmap)), bitmap);
        }
        return bitmap;
    }

    private void ˎ(Integer num, Bitmap bitmap) {
        NavigableMap ˋ = ˋ(bitmap.getConfig());
        Integer num2 = (Integer) ˋ.get(num);
        if (num2 == null) {
            throw new NullPointerException("Tried to decrement empty size, size: " + num + ", removed: " + ˋ(bitmap) + ", this: " + this);
        } else if (num2.intValue() == 1) {
            ˋ.remove(num);
        } else {
            ˋ.put(num, Integer.valueOf(num2.intValue() - 1));
        }
    }

    private NavigableMap<Integer, Integer> ˋ(Config config) {
        NavigableMap<Integer, Integer> navigableMap = (NavigableMap) this.ʻ.get(config);
        if (navigableMap != null) {
            return navigableMap;
        }
        NavigableMap treeMap = new TreeMap();
        this.ʻ.put(config, treeMap);
        return treeMap;
    }

    public String ˋ(Bitmap bitmap) {
        return ॱ(aau.ˏ(bitmap), bitmap.getConfig());
    }

    public String ˎ(int i, int i2, Config config) {
        return ॱ(aau.ॱ(i, i2, config), config);
    }

    public int ॱ(Bitmap bitmap) {
        return aau.ˏ(bitmap);
    }

    public String toString() {
        StringBuilder append = new StringBuilder().append("SizeConfigStrategy{groupedMap=").append(this.ᐝ).append(", sortedSizes=(");
        for (Entry entry : this.ʻ.entrySet()) {
            append.append(entry.getKey()).append('[').append(entry.getValue()).append("], ");
        }
        if (!this.ʻ.isEmpty()) {
            append.replace(append.length() - 2, append.length(), "");
        }
        return append.append(")}").toString();
    }

    static String ॱ(int i, Config config) {
        return "[" + i + "](" + config + ")";
    }

    private static Config[] ˏ(Config config) {
        if (VERSION.SDK_INT >= 26 && Config.RGBA_F16.equals(config)) {
            return ˎ;
        }
        switch (AnonymousClass3.ˊ[config.ordinal()]) {
            case 1:
                return ˋ;
            case 2:
                return ˊ;
            case 3:
                return ॱ;
            case 4:
                return ˏ;
            default:
                return new Config[]{config};
        }
    }
}
